package com.chenfan.mcn.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenfan.common.exception.BusinessException;
import com.chenfan.mcn.clients.privilege.PrivilegeClient;
import com.chenfan.mcn.clients.privilege.SUser;
import com.chenfan.mcn.dao.AeExcutorConfirmMapper;
import com.chenfan.mcn.dao.AeQuarterlyTableMapper;
import com.chenfan.mcn.enums.AeCommonEnum;
import com.chenfan.mcn.enums.ApprovalEnum;
import com.chenfan.mcn.exception.McnErrorCode;
import com.chenfan.mcn.model.AeExcutorConfirm;
import com.chenfan.mcn.model.AeQuarterlyTable;
import com.chenfan.mcn.model.dto.AeExcutorConfirmNewDTO;
import com.chenfan.mcn.service.AeExcutorConfirmService;
import com.chenfan.mcn.service.ApprovalFlowService;
import com.chenfan.mcn.utils.CommonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author chenguopeng
 * @Date 2021/6/17 14:36
 */
@Service
public class AeExcutorConfirmServiceImpl extends ServiceImpl<AeExcutorConfirmMapper, AeExcutorConfirm>
        implements AeExcutorConfirmService {

    @Autowired
    private ApprovalFlowService approvalFlowService;

    @Autowired
    private AeQuarterlyTableMapper aeQuarterlyTableMapper;

    @Autowired
    private PrivilegeClient privilegeClient;

    @Override
    public void batchCreate(AeExcutorConfirmNewDTO aeExcutorConfirmNewDTO) {
        List<Long> executorIds = aeExcutorConfirmNewDTO.getExcutorIds();
        if (CollectionUtils.isEmpty(executorIds)) {
            return;
        }
        // 判断是否存在
        List<AeExcutorConfirm> aeExecutorConfirms = this.baseMapper
                .selectList(Wrappers.lambdaQuery(AeExcutorConfirm.class).in(AeExcutorConfirm::getExcutorId, executorIds)
                        .eq(AeExcutorConfirm::getType, aeExcutorConfirmNewDTO.getType())
                        .eq(AeExcutorConfirm::getAeQuarterlyId, aeExcutorConfirmNewDTO.getAeQuarterlyId()));
        Map<Long, Long> aeExecutorMap = aeExecutorConfirms.stream().collect(
                Collectors.toMap(AeExcutorConfirm::getExcutorId, AeExcutorConfirm::getExcutorId, (key1, key2) -> key2));
        List<AeExcutorConfirm> list = new ArrayList<>(executorIds.size());
        AeExcutorConfirm aeExcutorConfirm = new AeExcutorConfirm();
        aeExcutorConfirm.setAeQuarterlyId(aeExcutorConfirmNewDTO.getAeQuarterlyId());
        aeExcutorConfirm.setType(aeExcutorConfirmNewDTO.getType());
        aeExcutorConfirm.setIsConfirmed(aeExcutorConfirmNewDTO.getIsConfirmed());
        aeExcutorConfirm.setIsConfirmed(AeCommonEnum.NOT_CONFIRM.getCode());
        executorIds.forEach(x -> {
            //如果存在就不新增了
            if (!aeExecutorMap.containsKey(x)) {
                AeExcutorConfirm newAeExecutorConfirm = new AeExcutorConfirm();
                BeanUtils.copyProperties(aeExcutorConfirm, newAeExecutorConfirm);
                newAeExecutorConfirm.setExcutorId(x);
                newAeExecutorConfirm.ignore();
                list.add(newAeExecutorConfirm);
            }
        });
        this.saveOrUpdateBatch(list);
    }

    /**
     * 确认状态修改
     *
     * @param aeExcutorConfirmNewDTO {@link AeExcutorConfirmNewDTO}
     */
    @Override
    public void update(AeExcutorConfirmNewDTO aeExcutorConfirmNewDTO) {
        aeExcutorConfirmNewDTO.setIssueDate(new Date());
        this.baseMapper.updateConfirm(aeExcutorConfirmNewDTO);
    }

    /**
     * 发送提成单
     *
     * @param id {@link Long}
     */
    @Override
    public void sendCommission(Long id) {
        if (Objects.isNull(id)) {
            throw new BusinessException(McnErrorCode.AE_CONFIRM_ID_IS_NULL);
        }
        AeExcutorConfirm aeExcutorConfirm = this.baseMapper.selectById(id);
        AeQuarterlyTable aeQuarterlyTable = aeQuarterlyTableMapper.selectById(aeExcutorConfirm.getAeQuarterlyId());
        Long executorId = aeExcutorConfirm.getExcutorId();
        SUser sUser = Objects.requireNonNull(privilegeClient.getUserById(executorId)).getObj();
        String executorName = Objects.nonNull(sUser) ? sUser.getUsername() : null;
        String aeQuarterlyCode = CommonUtil.getAeQuarterlyCode(aeQuarterlyTable.getAeQuarterlyCode());
        if (AeCommonEnum.IS_EXCUTOR.getCode().equals(aeExcutorConfirm.getType())) {
            approvalFlowService.sendAeNotify(aeQuarterlyTable.getTenantId(), aeQuarterlyTable.getId(),
                    aeQuarterlyTable.getAeQuarterlyExcuteCode(), aeQuarterlyCode,
                    ApprovalEnum.AE_COMMISSION, true, executorId, executorName, AeCommonEnum.IS_EXCUTOR.getCode());
        }
        if (AeCommonEnum.IS_DEPPRINCIPALNAMEID.getCode().equals(aeExcutorConfirm.getType())) {
            approvalFlowService.sendAeNotify(aeQuarterlyTable.getTenantId(), aeQuarterlyTable.getId(),
                    aeQuarterlyTable.getAeQuarterlyExcuteCode(), aeQuarterlyCode,
                    ApprovalEnum.AE_DEPARTMENT, true, executorId, executorName, AeCommonEnum.IS_DEPPRINCIPALNAMEID.getCode());
        }
        AeExcutorConfirm updateAeExcutorConfirm = new AeExcutorConfirm();
        updateAeExcutorConfirm.setId(aeExcutorConfirm.getId());
        updateAeExcutorConfirm.setIsConfirmed(AeCommonEnum.NOT_CONFIRM.getCode());
        updateAeExcutorConfirm.setIssueDate(null);
        this.baseMapper.updateById(updateAeExcutorConfirm);
    }

}
